package com.lotter.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.lotter.service.LoginService;


/**
 * 登录controller
 * ClassName: LoginController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年5月24日 下午3:15:19 <br/>
 *
 * @author panguixiang
 * @version
 */
@Controller
@RequestMapping("/common/login")
public class LoginController {
	
	@Autowired
	private LoginService loginService;
	
	/**
	 * 
	 * getLogin:(进入登录页面). <br/> 
	 *
	 * @author panguixiang
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value="page", method = RequestMethod.GET)
	public String getLogin() {
		return "login";
	}
	/**
	 * 
	 * login:(执行登录). <br/> 
	 *
	 * @author panguixiang
	 * @param username
	 * @param password
	 * @param request
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value="login", method = RequestMethod.POST)
	public String login(@RequestParam("username") String username,
			@RequestParam("password") String password,HttpServletRequest request) {
		if(loginService.login(username, password)) {//登录成功
			HttpSession session = request.getSession(true);
			session.setAttribute("username", username);
			if(username.equals("adminUser") || username.equals("adminPan")  || username.equals("adminJun") ) {
				session.setAttribute("usertype", 1);
			} else {
				session.setAttribute("usertype", 2);
			}
			return "redirect:/adminMange/main";
		} else {//登录失败
			return "redirect:/common/login/page";
		}
	}
	/**
	 * 
	 * login:(退出登录). <br/> 
	 *
	 * @author panguixiang
	 * @param request
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value="logout", method = RequestMethod.GET)
	public String login(HttpServletRequest request) {
		request.getSession(true).invalidate();
		return "redirect:/common/login/page";
	}
	
	@RequestMapping(value="index", method = RequestMethod.GET)
	public String getindex() {
		return "index";
	}
}
